package cn.tedu.boot22.controller;

import cn.tedu.boot22.entity.Hero;
import cn.tedu.boot22.mapper.HeroMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wxd
 * @version 1.0
 */
@Controller
public class HeroController {
    /**
     * 自动装配注解的作用:Spring框架结合Mybatis框架会自动将HeroMapper生成一个实现类和实现里面的方法,而且会自动实例化该对象
     * ,@Autowired(required = false) required = false 告诉idea编译器此对象是非必须的
     */
    @Autowired
    HeroMapper mapper;

    @RequestMapping("/addHero")
    @ResponseBody
    public String addHero(Hero hero) {
        System.out.println("hero = " + hero);
        // 此方法相当于执行的jdbc代码
        mapper.insert(hero);
        return hero.toString();
    }

    @RequestMapping("/deleteHero")
    @ResponseBody
    public String deleteHero(String name) {
        mapper.deleteByName(name);
        return "删除成功!";
    }

    @RequestMapping("/updateHero")
    @ResponseBody
    public String updateHero(Hero hero) {
        mapper.update(hero);
        return "修改完毕!";
    }

    @RequestMapping("/queryHero")
    @ResponseBody
    public String queryHero() {
        List<Hero> list = mapper.select();
        return list.toString();
    }
}
